package cc.dawning.utiming.lklocation.location.utils;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import java.io.File;

import cc.vv.baselibrary.util.LKToastUtil;

/**
 * Created by 025 on 2018/9/28.
 */

public class OpenOtherMapUtil {

    private static OpenOtherMapUtil mInstance;

    public static OpenOtherMapUtil getInstance() {
        if (null == mInstance) {
            mInstance = new OpenOtherMapUtil();
        }
        return mInstance;
    }

    /**
     * 打开百度地图
     *
     * @param slat  开始地点 维度
     * @param slon  开始地点 经度
     * @param sname 开始地点 名字
     * @param dlat  终点地点 维度
     * @param dlon  终点地点 经度
     * @param dname 终点名字
     * @param city  所在城市- 动态获取 （例如：北京市）
     */
    public void openBaiDuMap(Context context, double slat, double slon, String sname,
                             double dlat, double dlon, String dname, String city) {
        try {
            if (isInstallPackage("com.baidu.BaiduMap")) {
                if (context != null
                        && !TextUtils.isEmpty(sname)
                        && !TextUtils.isEmpty(dname)
                        && !TextUtils.isEmpty(city)) {
                    String uri = getBaiduMapUri(String.valueOf(slat), String.valueOf(slon), sname,
                            String.valueOf(dlat), String.valueOf(dlon), dname, city, "");
                    Intent intent = Intent.parseUri(uri, 0);
                    context.startActivity(intent); //启动调用
                }
            } else {
                LKToastUtil.showToastShort("您还未安装百度地图,请先去应用市场下载安装");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getBaiduMapUri(String originLat, String originLon, String originName, String desLat, String desLon, String destination, String region, String src) {
        String uri = "intent://map/direction?origin=latlng:%1$s,%2$s|name:%3$s" +
                "&destination=latlng:%4$s,%5$s|name:%6$s&mode=driving&region=%7$s&src=%8$s#Intent;" +
                "scheme=bdapp;package=com.baidu.BaiduMap;end";
        return String.format(uri, originLat, originLon, originName, desLat, desLon, destination, region, src);
    }


    /**
     * openGaoDeMap void 调用高德地图apk
     * http://lbs.amap.com/api/uri-api/guide/travel/route
     */
    public void openGaoDeMap(Context context,
                             String dqAddress, String gotoAddress,
                             String gotoLatitude, String gotoLongitude) {
        try {
            if (isInstallPackage("com.autonavi.minimap")) {
                if (context != null
                        && !TextUtils.isEmpty(gotoAddress)
                        && !TextUtils.isEmpty(gotoLatitude)
                        && !TextUtils.isEmpty(gotoLongitude)) {
                    String url = "androidamap://navi?sourceApplication=" + dqAddress
                            + "&poiname=" + gotoAddress + "&lat=" + gotoLatitude + "&lon=" + gotoLongitude + "&dev=0&style=2";
                    Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(url));
                    intent.setPackage("com.autonavi.minimap");
                    context.startActivity(intent);
                }
            } else {
                LKToastUtil.showToastShort("您还未安装高德地图,请先去应用市场下载安装");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isInstallPackage(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }


    /**
     * 打开腾讯地图
     * params 参考http://lbs.qq.com/uri_v1/guide-route.html
     *
     * @param context
     * @param dqAddress
     * @param gotoAddress
     * @param gotoLatitude
     * @param gotoLongitude 驾车：type=drive，policy有以下取值
     *                      0：较快捷
     *                      1：无高速
     *                      2：距离
     *                      policy的取值缺省为0
     *                      &from=" + dqAddress + "&fromcoord=" + dqLatitude + "," + dqLongitude + "
     */
    public void openTencentMap(Context context, String dqAddress,
                               String gotoAddress, String gotoLatitude, String gotoLongitude) {
        try {
            if (isInstallPackage("com.tencent.map")) {
                if (context != null && !TextUtils.isEmpty(dqAddress)
                        && !TextUtils.isEmpty(gotoAddress)
                        && !TextUtils.isEmpty(gotoLatitude)
                        && !TextUtils.isEmpty(gotoLongitude)) {
                    String url1 = "qqmap://map/routeplan?type=drive&to=" + gotoAddress +
                            "&tocoord=" + gotoLatitude + "," + gotoLongitude + "&policy=2&referer=myapp";
                    Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(url1));
                    context.startActivity(intent);
                }
            } else {
                LKToastUtil.showToastShort("您还未安装腾讯地图,请先去应用市场下载安装");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}